use std::{sync::Mutex, collections::HashMap};
use once_cell::sync::Lazy;

use crate::{connection, request, message};

static ROUTER: Lazy<Mutex<HashMap<u32,fn(&mut connection::Conn, message::Message)>>> = Lazy::new(|| {
    let mut m = HashMap::new();
    Mutex::new(m)
});

pub fn regist(id:u32, func:fn(&mut connection::Conn, message::Message)) {
    ROUTER.lock().unwrap().insert(id, func);
}

pub fn handle(conn:&mut connection::Conn, msg:message::Message) {
    ROUTER.lock().unwrap().get(&msg.id).unwrap()(conn, msg)
}